// pages/wantItems/wantItems.js
// myWants.js
Page({
    data: {
      wants: [],              // 想要的产品列表
      page: 1,                // 当前页码
      pageSize: 10,           // 每页数量
      total: 0,               // 总数量
      loading: false,         // 加载中状态
      noMoreWants: false,     // 没有更多产品
      userInfo: null          // 用户信息
    },
  
    onLoad() {
      this.checkUserInfo();
      this.loadMyWants();
    },
  
    onShow() {
      // 页面显示时重新加载想要的产品，确保数据最新
      this.loadMyWants(true);
    },
  
    onPullDownRefresh() {
      // 下拉刷新
      this.loadMyWants(true);
      wx.stopPullDownRefresh();
    },
  
    onReachBottom() {
      // 触底加载更多
      if (!this.data.loading && !this.data.noMoreWants) {
        this.loadMyWants();
      }
    },
  
    // 检查用户信息
    checkUserInfo() {
      const userInfo = wx.getStorageSync('userInfo');
      if (userInfo) {
        this.setData({ userInfo });
      } else {
        wx.navigateTo({ url: '/pages/login/index' });
      }
    },
  
    // 加载我想要的产品
    loadMyWants(refresh = false) {
      if (refresh) {
        this.setData({
          page: 1,
          wants: [],
          noMoreWants: false
        });
      }
  
      if (this.data.loading || this.data.noMoreWants) return;
  
      this.setData({ loading: true });
  
      wx.cloud.callFunction({
        name: 'getMyWants',
        data: {
          userId: this.data.userInfo._id,
          page: this.data.page,
          pageSize: this.data.pageSize
        },
        success: res => {
          this.setData({ loading: false });
          console.log(res);
          if (res.result && res.result.success) {
            const newWants = res.result.data.list;
            const total = res.result.data.total;
            
            this.setData({
              wants: refresh ? newWants : [...this.data.wants, ...newWants],
              page: this.data.page + 1,
              total: total
            });
            
            // 判断是否还有更多产品
            this.setData({
              noMoreWants: this.data.wants.length >= total
            });
          } else {
            wx.showToast({
              title: res.result.message || '加载产品失败',
              icon: 'none'
            });
          }
        },
        fail: err => {
          this.setData({ loading: false });
          console.error('加载我想要的产品失败', err);
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        }
      });
    },
  
    // 前往产品详情页
    goToProductDetail(e) {
      const productId = e.currentTarget.dataset.productId;
      wx.navigateTo({
        url: `/pages/products/products?productId=${productId}`
      });
    },
  
    // 取消想要
    cancelWant(e) {
        console.log(e);
      const wantId = e.currentTarget.dataset.wantId;
      const want = this.data.wants.find(item => item._id === wantId);
      console.log(want);
      wx.showModal({
        title: '提示',
        content: '确定要取消想要该产品吗？',
        success: async (res) => {
          if (res.confirm) {
            wx.showLoading({ title: '处理中...' });
            
            try {
              await wx.cloud.callFunction({
                name: 'cancelWant',
                data: {
                  wantId: wantId,
                  productId: want.productId
                }
              });
              
              // 重新加载想要的产品
              this.loadMyWants(true);
              wx.showToast({
                title: '已取消想要',
                icon: 'success'
              });
            } catch (error) {
              console.error('取消想要失败', error);
              wx.showToast({
                title: '取消失败',
                icon: 'none'
              });
            } finally {
              wx.hideLoading();
            }
          }
        }
      });
    }
  });